package com.web_bloks.widget.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;

/**
 * 
 * @author Jean-Lou Dupont
 *
 */
public class LoginLogoutWidget extends FlexTable { 

	public enum State {
		login, logout
	}
	
	ImgAnchor login=null, logout=null;
	
	String loginUrl=null, logoutUrl=null;
	
	State state = State.login;
	
	public LoginLogoutWidget() {
		super();
		setup();
	}
	
	private void setup() {
		WidgetImageBundle bundle = (WidgetImageBundle) GWT.create( WidgetImageBundle.class );

		Image ilogin = bundle.login().createImage();
		Image ilogout = bundle.logout().createImage();

		login  = GWT.create(ImgAnchor.class);
		logout = GWT.create(ImgAnchor.class);
		
		login.setImg( ilogin );
		logout.setImg( ilogout );
		
		final WidgetMessages MSG = (WidgetMessages) GWT.create(WidgetMessages.class);
		
		login.setTitle(MSG.login_logout_login());
		logout.setTitle(MSG.login_logout_logout());
		
		setWidget(0,0,login);
		setWidget(0,1,logout);
		
		setState( State.login );
	}
	
	public void setState(State state) {
		
		this.state = state;
		
		switch(state) {
		case login:
			login.setVisible(true);
			logout.setVisible(false);
			break;
		case logout:
			login.setVisible(false);
			logout.setVisible(true);
			break;
		default:
			throw new RuntimeException("invalid state");
		}
		setUrls();
	}
	
	public void setLoginUrl(String url) {
		loginUrl = new String(url);
	}
	
	public void setLogoutUrl(String url) {
		logoutUrl = new String(url);
	}
	
	/*****************************************
	 * HELPERS 
	 *****************************************/
	protected void setUrls() {
		if ( null != loginUrl ) {
			login.setHref(loginUrl);
		}
		if ( null != logoutUrl ) {
			logout.setHref(logoutUrl);
		}
	}
}//END